﻿namespace ORS.OrderRobot.Cache
{
	public interface ICache
	{
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="value">缓存值</param>
        /// <param name="expiration">过期时间</param>
        Task SetAsync(string key, object value, TimeSpan? expiration = null);

        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <typeparam name="T">缓存值类型</typeparam>
        /// <param name="key">缓存键</param>
        Task<T> GetAsync<T>(string key);

        /// <summary>
        /// 获取缓存，如果不存在则通过回调函数创建并缓存
        /// </summary>
        /// <typeparam name="T">缓存值类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="factory">数据获取回调函数</param>
        /// <param name="expiration">过期时间</param>
        Task<T> GetOrCreateAsync<T>(string key, Func<Task<T>> factory, TimeSpan? expiration = null);

        /// <summary>
        /// 移除缓存
        /// </summary>
        /// <param name="key">缓存键</param>
        Task RemoveAsync(string key);

        /// <summary>
        /// 获取缓存键
        /// </summary>
        /// <param name="prefix">如不指定则获取所有的</param>
        /// <returns></returns>
        Task<IEnumerable<string>> GetAllKeysAsync(string? prefix = null);
    }

    /// <summary>
    /// 缓存时间常量
    /// </summary>
    public static class CacheExpiration
    {
        /// <summary>
        /// 1分钟
        /// </summary>
        public static readonly TimeSpan OneMinute = TimeSpan.FromMinutes(1);

        /// <summary>
        /// 5分钟
        /// </summary>
        public static readonly TimeSpan FiveMinutes = TimeSpan.FromMinutes(5);

        /// <summary>
        /// 10分钟
        /// </summary>
        public static readonly TimeSpan TenMinutes = TimeSpan.FromMinutes(10);

        /// <summary>
        /// 30分钟
        /// </summary>
        public static readonly TimeSpan ThirtyMinutes = TimeSpan.FromMinutes(30);

        /// <summary>
        /// 1小时
        /// </summary>
        public static readonly TimeSpan OneHour = TimeSpan.FromHours(1);

        /// <summary>
        /// 1天
        /// </summary>
        public static readonly TimeSpan OneDay = TimeSpan.FromDays(1);

        /// <summary>
        /// 1周
        /// </summary>
        public static readonly TimeSpan OneWeek = TimeSpan.FromDays(7);
    }
}

